//
//  MNDHomeViewController.m
//  miniwangdai2
//
//  Created by 谭伟 on 2017/4/5.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "MNDHomeViewController.h"
#import "MNDGuideViewController.h"
#import "MNDWebViewController.h"
#import "JhtVerticalMarquee.h"
#import "MNDTopCycleView.h"
#import "MNDMessageViewController.h"
#import "MNDMessageModel.h"
#import "MNDMyInfoOrLoginView.h"
#import "MNDUserAssetModel.h"
#import "MNDLoginUserModel.h"
#import "MNDTransactionRecordViewController.h"
#import "MNDMyIncomeModel.h"
#import "MNDUserAssetViewController.h"
#import "MNDInvestmentRecordViewController.h"
#import "MNDReturnListViewController.h"
#import "MNDMyInfoModel.h"
#import "MNDRechargeViewController.h"
#import "SYRefreshControl.h"
#import "MNDShareWebViewController.h"
#import "MNDSlideUpViewController.h"
#import "MNDBackMoneyRootViewController.h"
#import "SYRefreshControl.h"
#import "MNDBBSViewController.h"
#import "RSAUtil.h"
#import "MNDLatestReturnWebViewController.h"
#import "MNDInfoDisclosureViewController.h"
#import "MNDRecordGroupViewController.h"

typedef NS_ENUM(NSInteger, BtnType) {
    BtnTypeGuide = 100,    //新手指南
    BtnTypePlatformData,   //平台数据
    BtnTypeInfoDisclosure, //信息披露
    BtnTypeShareApp        //邀请
};

@interface MNDHomeViewController () <UIScrollViewDelegate>

{
    // 是否暂停了上下滚动的跑马灯
    BOOL _isPauseV;
}
//顶部轮播图
@property (weak, nonatomic) IBOutlet MNDTopCycleView *topCycleView;
//登录信息显示
@property (weak, nonatomic) IBOutlet UIView *loginInfoView;
//登录信息视图的高度约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginViewHeight;
//@property (weak, nonatomic) IBOutlet UIView *bottomView;
//文字跑马灯
@property (weak, nonatomic) IBOutlet UIView *cycleLabelView;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UIButton *goInvestBtn;
/* time */
@property (assign, nonatomic) BOOL saveTime;

/* 消息数组 */
@property (strong, nonatomic) NSMutableArray<MNDMessageModel*> *messageModelArr;
/* 消息标题数组 */
@property (strong, nonatomic) NSMutableArray *messageModelTitleArr;
/* 跑马灯 */
@property (strong, nonatomic) JhtVerticalMarquee *verticalMarquee;
/* 首页登录后我的信息 */
@property (strong, nonatomic) MNDMyInfoOrLoginView *logInfoView;
/* 首页未登录 */
@property (strong, nonatomic) MNDMyInfoOrLoginView *nologInfoView;
//@property (weak, nonatomic) IBOutlet UIView *bottomView;

//scrollview
@property (weak, nonatomic) IBOutlet UIScrollView *homeScrollView;
//我的资产
@property (strong, nonatomic) MNDUserAssetModel *userAssetInfo;

@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

//webviewVC
@property (strong, nonatomic) MNDWebViewController *webviewVC;
//总资产记录
@property (strong, nonatomic) NSString *allMoneyStr;

//隐藏总资产
@property (assign, nonatomic) BOOL hideAllmoney;

@property (strong,nonatomic) MNDMyIncomeModel *myIncomeModel;

//@property (weak, nonatomic) IBOutlet UIView *shadowView;

@property (weak, nonatomic) IBOutlet UILabel *yearAprLabel;

@property (weak, nonatomic) IBOutlet UIImageView *naviBackgroundView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *barnerHeight;

@property (weak, nonatomic) IBOutlet UIView *bottomSlideUpView;

@property (nonatomic) BOOL stopAnimation;

/**
 用户资产请求ID
 */
@property(nonatomic, strong) NSString *userAssetRequestId;

@end

@implementation MNDHomeViewController

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //适配iOS11
    if (@available(iOS 11.0, *)) {
        self.homeScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    //导航栏透明
    self.naviBarTranslucent = YES;
    
    self.makeFullScreen = NO;
    
    NSDictionary *appkey = [UserDefaultsTool objectForKey:@"AppKey"];
    if (appkey && appkey.count > 0) {
        [self loadData];
    } else {
        [self requestSecretKey];
    }
    
    if (kScreenWidth <= 320) {
        [self.naviBackgroundView setImage:[UIImage imageNamed:@"home_navi_bar_bg_640"]];
    }
    self.naviBackgroundView.alpha = 0.0;
    
    if (kScreenWidth <= 320) {
        self.barnerHeight.constant = 140;
    }
    if (kScreenWidth >= 414) {
        self.barnerHeight.constant = 185;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoMessageVC:) name:kGoToMessageVC object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserBalance:) name:@"update_user_balance" object:nil];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSDictionary *appkey = [UserDefaultsTool objectForKey:@"AppKey"];
    
    self.userAssetRequestId = nil;

    if ([MNDLoginUserModel isUserLogin]) {//已登录
        
        if (kScreenWidth > 414) {
            self.loginViewHeight.constant = 208;
        }else {
            self.loginViewHeight.constant = 200;
        }
        if (appkey && appkey.count > 0) {
            [self loginState];
            [self loadMyInfo];
        }
        self.logInfoView.hidden = NO;
        self.nologInfoView.hidden = YES;
        
    } else {//未登录
        if (kScreenWidth <= 375) {
            self.loginViewHeight.constant = 1;
        } else {
            self.loginViewHeight.constant = 60;
        }
        
        self.logInfoView.hidden = YES;
        self.nologInfoView.hidden = NO;
    }
    
    if (kDevice_iPad) {
        if ([MNDLoginUserModel isUserLogin]) {
            [self.bottomSlideUpView setHidden: NO];
        } else {
            [self.bottomSlideUpView setHidden: YES];
        }
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // 如果暂停了，使用继续方式开启
    if (_isPauseV) {
        [_verticalMarquee marqueeOfSettingWithState:MarqueeContinue_V];
    }
    
    if (self.userAssetRequestId) {
        [XMCenter cancelRequest:self.userAssetRequestId];
    }
}

#pragma mark - 获取APPKey
- (void)requestSecretKey {
    WeakSelf(self);
    [APIManager sendRequestWithPath:API_GET_APPKEY parameters:nil method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [UserDefaultsTool saveObject:responseModel.data forKey:@"AppKey"];
            [weakself loadData];
            [weakself loginState];
        }
    } onFailure:^(NSError *error) {
        
    }];
}

#pragma mark -搭建界面
- (void)setupUI {
    WeakSelf(self);
    [SYRefreshControl configGifMJRefresh:self.homeScrollView refreshType:MJRefreshSupportRefresh beginRefresh:NO timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{
        [weakself loadData];
    } loadMoreBlock:nil];
    self.homeScrollView.delegate = self;
    self.moneyLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:27.0];
    self.yearAprLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:27.0];
    //设置年化率不同的字体
    NSString *str = @"10~13";
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Light" size:12.0] range:NSMakeRange(2, 1)];
    self.yearAprLabel.attributedText = attributeStr;
    self.navigationItem.title = @"";

    self.goInvestBtn.clipsToBounds = YES;
    
    //圆角
    self.goInvestBtn.layer.cornerRadius = 20.0;
//    self.goInvestBtn.clipsToBounds = YES;
    
    // 添加上下滚动的跑马灯
    [self addVerticalMarquee];
    
    //定义等待执行的block，轮播图跳转网页
    void(^clickCycleImgBlock)(MNDHomeCycleModel *cycleModel) = ^(MNDHomeCycleModel *cycleModel) {
        //如果是邀请好友
        if ([cycleModel.title isEqualToString:@"邀请好友"]) {
            if ([MNDLoginUserModel isUserLogin]) {
                [weakself loadEncodeUserCodeWithPage:@"share_friend"];
            } else {
                [weakself goToLoginVC];
            }
        }else {
            MNDWebViewController *mndWebViewController = [[MNDWebViewController alloc]init];
            mndWebViewController.hidesBottomBarWhenPushed = YES;
            mndWebViewController.webViewUrl = cycleModel.the_chain;
            if(cycleModel.title.length > 0) {
                mndWebViewController.title = cycleModel.title;
            }else {
                mndWebViewController.naviBarHidden = YES;
            }
            [weakself.navigationController pushViewController:mndWebViewController animated:YES];
        }
    };
    self.topCycleView.clickCycleImgBlock = clickCycleImgBlock;
    
    //底部刷新视图创建
    //登录状态改变页面
    NSArray *loginViews =[[NSBundle mainBundle]loadNibNamed:@"MNDMyInfoOrLoginView" owner:self options:nil];
    //已登陆
    self.logInfoView = loginViews[1];
    [self.loginInfoView addSubview:self.logInfoView];
    [self.logInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.loginInfoView);
    }];
    //点击账单
    [self.logInfoView.billMoneyBtn addTarget:self action:@selector(clickBillBtn:) forControlEvents:UIControlEventTouchUpInside];
//    self.bottomView.hidden = NO;
    //点击总资产
    self.logInfoView.allMoneyLabel.userInteractionEnabled = YES;
    [self.logInfoView.allMoneyLabel addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBackImgV:)]];
    //已赚收益
    [self.logInfoView.latestIncomeButton addTarget:self action:@selector(clickLatest30DaysIncomeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //点击隐藏显示总资产
    [self.logInfoView.showOrHidenMoneyBtn addTarget:self action:@selector(clickShowOrHidenMoneyBtn:) forControlEvents:UIControlEventTouchUpInside];
    //点击账户余额
    [self.logInfoView.accountRemainView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAccountRemainView:)]];
    //点击刷新余额
    [self.logInfoView.refreshBalanceBtn addTarget:self action:@selector(refreshBalanceAction:) forControlEvents:UIControlEventTouchUpInside];
    //点击当前投资
    [self.logInfoView.currentInvestMoneyView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCurrentInvestView:)]];
    //点击今日回款
    [self.logInfoView.backMoneyView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBackMoneyView:)]];
    //点击充值
    [self.logInfoView.rechargeButton addTarget:self action:@selector(rechargeAction) forControlEvents:UIControlEventTouchUpInside];
    
    //未登录
//    self.nologInfoView = loginViews[0];
//    [self.loginInfoView addSubview:self.nologInfoView];
//    [self.nologInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.bottom.equalTo(self.loginInfoView);
//    }];
    
//    [self.bottomView addSubview:loginViews[2]];
    
    //创建登录状态view
//     MNDMyInfoOrLoginView *infoView = [[MNDMyInfoOrLoginView alloc]init];
    
   //账单点击事件
}
#pragma mark - 刷新余额

- (void)refreshBalanceAction:(UIButton *)sender {
    self.stopAnimation = NO;
    [self startRotation:sender];
    [self loginState];
}

- (void)startRotation:(UIButton *)button {
    
    [UIView animateWithDuration:1/3.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        button.transform = CGAffineTransformRotate(button.transform, -M_PI_2);
    } completion:^(BOOL finished) {
        if (!self.stopAnimation) {
            [self startRotation:button];
        }
    }];
}

#pragma mark - UIScrollView Delegate

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
    if (kDevice_iPad && ![MNDLoginUserModel isUserLogin]) {
        return;
    }
    CGFloat presentHeight = scrollView.contentOffset.y - (scrollView.contentSize.height - kScreenHeight);
    
    if (presentHeight >= 50) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
            MNDSlideUpViewController *slideVC = [storyboard instantiateViewControllerWithIdentifier:[MNDSlideUpViewController description]];
            
            [self presentViewController:slideVC animated:YES completion:^{
                
            }];
        });
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY > 170/2) {
        self.naviBackgroundView.alpha = MIN((offsetY - 85)/20, 1.0);
    } else {
        self.naviBackgroundView.alpha = 0.0;
    }
}

#pragma mark - Custom Method

- (void)updateUserBalance:(NSNotification *)notification {
    ResponseModel *response = notification.object;
    
    self.userAssetInfo.userAmountAvailable = response.data[@"user_amount_available"];
    self.userAssetInfo.userAmountAssets = response.data[@"user_amount_assets"];
    [self configUserProfitInfo:self.userAssetInfo];
}

- (void)loginState{
    if ([MNDLoginUserModel isUserLogin]) {//已登陆
//        self.loginViewHeight.constant = 200;
        //个人资产数据获取
        __weak typeof(self) weakSelf = self;
        
        self.userAssetRequestId = [APIManager sendRequestWithPath:API_USER_ASSET_LIST parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
            if (responseModel.status == ResponseSuccess) {
                weakSelf.userAssetInfo = [MNDUserAssetModel mj_objectWithKeyValues:responseModel.data];
                
                if (weakSelf.logInfoView.showOrHidenMoneyBtn.selected == NO) {
                    //已赚收益
                    NSString *actualProfit = weakSelf.userAssetInfo.userInterestAmount;
                    if (actualProfit.doubleValue <= 0) {
                        actualProfit = @"0.00";
                    }
                    weakSelf.logInfoView.currentYieldLabel.text = actualProfit;
                    //待收收益
                    NSNumber *amount = [NSNumber numberWithString:weakSelf.userAssetInfo.userWaitInterestAmount];
                    NSString *returnAmount = [NSString stringWithFormat:@"%@", amount];
                    if (returnAmount.doubleValue <= 0) {
                        returnAmount = @"0.00";
                    }
                    weakSelf.logInfoView.todayMoneyLabel.text = returnAmount;
                    
                } else {
                    weakSelf.logInfoView.currentYieldLabel.text = @"****";
                    weakSelf.logInfoView.todayMoneyLabel.text = @"****";
                }
                
                [UserDefaultsTool saveObject:weakSelf.userAssetInfo.userAmountAvailable forKey:@"usableMoney"];
                [weakSelf configUserProfitInfo:weakSelf.userAssetInfo];
            } else {
                NSDictionary *dic = responseModel.msg;
                [NetworkProgressHUD showHudMsg:dic[@"error"]];
            }
        } onFailure:^(NSError *error) {
            NSLog(@"error: %@", error);
            [weakSelf.refreshHeader endRefreshing];
            [NetworkProgressHUD showHudNetworkError:error];
        }];

    }else {//未登录
//        [self loadMyInfo];
    }
//    [self.refreshHeader endRefreshing];
}
#pragma mark -加载数据

- (void)loadData {
    NSMutableArray *messageModelArr = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *messageModelTitleArr = [NSMutableArray arrayWithCapacity:0];
    
    WeakSelf(self);
    //首页剩余可投金额
    [APIManager sendRequestWithPath:API_HOME_DATA parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            //            NSDictionary *dict = [responseObject yy_modelToJSONObject];
            NSDictionary *data = responseModel.data;
            //            MNDMyInfoModel *myInfoModel = [MNDMyInfoModel yy_modelWithJSON:data];
            NSString *rateStr = data[@"interest_rate"];
            self.yearAprLabel.text = rateStr;
            id surplusmoney = data[@"surplus_money"];
            NSString *moneyStr = @"0.00";
            //            if ([money isKindOfClass:[NSNumber class]]) {
            //                moneyStr = [NSString stringWithFormat:@"%@",money];
            //            } else {
            //
            //            }
            moneyStr = [NSString stringWithFormat:@"%@", surplusmoney];
            if (moneyStr.integerValue == 0) {
                [weakself.goInvestBtn setTitle:@"暂无新标" forState:UIControlStateNormal];
                
                [weakself.goInvestBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
                [weakself.goInvestBtn setBackgroundImage:[UIImage imageNamed:@"no_invest_data"] forState:UIControlStateNormal];
                [weakself.goInvestBtn setBackgroundColor:kColorLightGray];
                //                self.goInvestBtn.userInteractionEnabled = NO;
            }else {
                [weakself.goInvestBtn setTitle:@"立即出借" forState:UIControlStateNormal];
                [weakself.goInvestBtn setBackgroundImage:[UIImage imageNamed:@"invest_btn"] forState:UIControlStateNormal];
                
                //                self.goInvestBtn.userInteractionEnabled = NO;
            }
            
            moneyStr = [moneyStr stringByReplacingOccurrencesOfString:@"," withString:@""];
            float moneyNum = moneyStr.floatValue/10000;
            NSString *money = [NSString stringWithFormat:@"%.02f万", moneyNum];
            if (moneyStr.doubleValue <= 10000) {
                money = [NSString stringWithFormat:@"%.02f元", moneyNum * 10000];
            }
            
            NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:money];
            
            NSRange range = [money rangeOfString:@"万"];
            if (moneyStr.doubleValue <= 10000) {
                range = [money rangeOfString:@"元"];
            }
            
            [attributeStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Light" size:12.0] range:range];
            
            weakself.moneyLabel.attributedText = attributeStr;
        }
        if (responseModel.status == ResponseFailure) {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        NSLog(@"error: %@", error);
        [weakself.refreshHeader endRefreshing];
        [NetworkProgressHUD showHudNetworkError:error];
    }];

    //消息列表
    [APIManager sendRequestWithPath:API_NOTICE_LIST parameters:[MNDSign signedParamWithDict:@{@"pagesize":@"2"}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            NSLog(@"data: %@", responseModel.data);
            NSDictionary *responseDict = [responseModel yy_modelToJSONObject];
            NSArray *responseArr = [responseDict objectForKey:@"data"];
            NSArray *messageArr = [NSArray yy_modelArrayWithClass:[MNDMessageModel class] json:responseArr];
            for ( MNDMessageModel *messageModel in messageArr) {
                if (messageModel) {
                    [messageModelArr addObject:messageModel];
                    [messageModelTitleArr addObject:messageModel.title];
                }
            }
            //赋值
            if (weakself.verticalMarquee.sourceArray) {
                
            }else {
                weakself.verticalMarquee.sourceArray = messageModelTitleArr;
            }
        }
        if (responseModel.status == ResponseFailure) {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
        weakself.messageModelArr = messageModelArr;
        [weakself.refreshHeader endRefreshing];
    } onFailure:^(NSError *error) {
        NSLog(@"error: %@", error);
        [weakself.refreshHeader endRefreshing];
        [NetworkProgressHUD showHudNetworkError:error];
    }];
    
    [SYRefreshControl configAfterLoading:self.homeScrollView hasData:YES];
    [self.topCycleView setupCycleScrollView];
}

#pragma mark 上下滚动的跑马灯
/** 添加上下滚动的跑马灯 */
- (void)addVerticalMarquee {
    [self.verticalMarquee setFrame:CGRectMake(40, 200, kScreenWidth - 40, 42)];
    [self.cycleLabelView addSubview:_verticalMarquee];
    [_verticalMarquee mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self.cycleLabelView);
        make.left.mas_equalTo(self.cycleLabelView).mas_offset(30);

    }];
    _verticalMarquee.backgroundColor = [UIColor whiteColor];
    _verticalMarquee.verticalTextColor = [UIColor colorWithRed:101/255.0 green:101/255.0 blue:101/255.0 alpha:1.0];
    _verticalMarquee.verticalTextFont = [UIFont systemFontOfSize:12.0];
    [_verticalMarquee scrollWithCallbackBlock:^(JhtVerticalMarquee *view, NSInteger currentIndex) {
//        NSLog(@"滚动到第 %ld 条数据", currentIndex);
    }];
    
    // 开始滚动
    [_verticalMarquee marqueeOfSettingWithState:MarqueeStart_V];
    // 给跑马灯添加点击手势
    UITapGestureRecognizer *vtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(verticalMarqueeTapGes:)];
    [_verticalMarquee addGestureRecognizer:vtap];
}

//点击文字跑马灯
- (void)verticalMarqueeTapGes:(UITapGestureRecognizer *)ges {
    NSLog(@"点击第 %ld 条数据啦啊！！！", (long)_verticalMarquee.index);
 
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
        
        MNDMessageViewController *messageVC = [storyboard instantiateViewControllerWithIdentifier:@"MNDMessageViewController"];
        messageVC.forumCode = self.messageModelArr[_verticalMarquee.index].forum_code;
        [self.navigationController pushViewController:messageVC animated:NO];
}


- (void)gotoMessageVC:(NSNotification *)notification {
    NSDictionary *userInfo = notification.object;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
    MNDMessageViewController *messageVC = [storyboard instantiateViewControllerWithIdentifier:@"MNDMessageViewController"];
    messageVC.forumCode = userInfo[@"forum_code"];
    [self.navigationController pushViewController:messageVC animated:NO];
}

- (IBAction)midBtnClick:(UIButton *)sender {
    MNDWebViewController *mndWebViewController = [[MNDWebViewController alloc]init];
    mndWebViewController.hidesBottomBarWhenPushed = YES;
    
    switch (sender.tag) {
        case BtnTypeGuide://新手指南
        {
            
            MNDShareWebViewController *shareVc = [[MNDShareWebViewController alloc] init];
            shareVc.webViewUrl = [NSString stringWithFormat:@"%@%@", WebBaseURL, kNewPeopleGuideURLStr];
            shareVc.naviBarHidden = YES;
            shareVc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:shareVc animated:YES];

        }
            break;
        case BtnTypePlatformData://平台数据-5.1版本换成平台介绍
        {

            mndWebViewController.hidesBottomBarWhenPushed = YES;
            mndWebViewController.webViewUrl = [NSString stringWithFormat:@"%@",kComPanyIntroduce];
            mndWebViewController.title = @"平台介绍";
            mndWebViewController.naviBarHidden = YES;
            [self.navigationController pushViewController:mndWebViewController animated:YES];
            
        }
            break;
        case BtnTypeInfoDisclosure://信息披露
        {
            MNDInfoDisclosureViewController *infoDiscVC = [[MNDInfoDisclosureViewController alloc] init];
            infoDiscVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:infoDiscVC animated:YES];
        }
            break;
        case BtnTypeShareApp://邀请有礼
        {
            
            if ([MNDLoginUserModel isUserLogin]) {
                [self loadEncodeUserCodeWithPage:@"share_friend"];
            } else {
                [self goToLoginVC];
            }
        }
            break;
            
        default:
            break;
    }
}

- (NSString *)rsaEncode:(NSString *)rsaStr {
    NSString *encryText = [RSAUtil encryptString:[NSString stringWithFormat:@"%@", rsaStr] publicKey:RSA_PUBLIC_KEY];
    NSString *unreserved = @"";
    NSMutableCharacterSet *allowed = [NSMutableCharacterSet alphanumericCharacterSet];
    [allowed addCharactersInString:unreserved];
    
    NSString *encodeEncryText = [encryText stringByAddingPercentEncodingWithAllowedCharacters:allowed];
    
    return encodeEncryText;
}

//点击立即投资按钮
- (IBAction)goInvest:(UIButton *)sender {
    self.tabBarController.selectedIndex = 1;
}


- (void)loadMyInfo {
    
//    WeakSelf(self);
//    [APIManager sendRequestWithPath:API_HOME_MYINFO parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
//        if (responseModel.status == ResponseSuccess) {
//            NSDictionary *dict = [responseModel yy_modelToJSONObject];
//            NSDictionary *dataDict = [dict objectForKey:@"data"];
//            self.myIncomeModel = [MNDMyIncomeModel yy_modelWithJSON:dataDict];
//            if (weakself.logInfoView.showOrHidenMoneyBtn.selected == NO) {
//                //已赚收益
//                NSString *actualProfit = weakself.userAssetInfo.userInterestAmount;
//                if (actualProfit.doubleValue <= 0) {
//                    actualProfit = @"0.00";
//                }
//                weakself.logInfoView.currentYieldLabel.text = actualProfit;
//                //待收收益
//                NSNumber *amount = [NSNumber numberWithString:weakself.userAssetInfo.userWaitInterestAmount];
//                NSString *returnAmount = [NSString stringWithFormat:@"%@", amount];
//                if (returnAmount.doubleValue <= 0) {
//                    returnAmount = @"0.00";
//                }
//                weakself.logInfoView.todayMoneyLabel.text = returnAmount;
//                
//            } else {
//                weakself.logInfoView.currentYieldLabel.text = @"****";
//                weakself.logInfoView.todayMoneyLabel.text = @"****";
//            }
//            
//        }else {
//            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
//        }
//    } onFailure:^(NSError *error) {
//        NSLog(@"error: %@", error);
//        [NetworkProgressHUD showHudNetworkError:error];
//    }];
}

//点击账单
- (void)clickBillBtn:(UIButton *)sender{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Transaction" bundle:nil];
    MNDTransactionRecordViewController *billVC = [storyboard instantiateViewControllerWithIdentifier:@"MNDTransactionRecordViewController"];
    billVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:billVC animated:YES];
}

//个人资产赋值
- (void)configUserProfitInfo:(MNDUserAssetModel *)assetModel {
    if (self.logInfoView.showOrHidenMoneyBtn.selected == NO) {
        if (assetModel.userAmountAssets.doubleValue > 0) {
            self.logInfoView.allMoneyLabel.text = assetModel.userAmountAssets;
        } else {
            self.logInfoView.allMoneyLabel.text = @"0.00";
        }
        //账户余额
        NSString *userAmount = assetModel.userAmountAvailable;
        if (userAmount.doubleValue <= 0) {
            userAmount = @"0.00";
        }
        self.logInfoView.accountMoneyLabel.text = userAmount;
        //当前投资(出借)
        NSString *tenderAmount = assetModel.userCurrentTenderAmount;
        if (tenderAmount.doubleValue <= 0) {
            tenderAmount = @"0.00";
        }
        self.logInfoView.currentInvestLabel.text = tenderAmount;
        //待收益
        NSString *profitStr = assetModel.userWaitInterestAmount;
        if (profitStr.doubleValue <= 0) {
            profitStr = @"0.00";
        }
        self.logInfoView.todayMoneyLabel.text = profitStr;
     
    } else {
        self.logInfoView.allMoneyLabel.text = @"****";
        self.logInfoView.accountMoneyLabel.text = @"****";
        self.logInfoView.currentInvestLabel.text = @"****";
        self.logInfoView.todayMoneyLabel.text = @"****";
    }
}

- (void)loadDepositInfo {
    
}


//去充值
- (void)rechargeAction {
    
    //如果开通存管，就去充值
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Recharge" bundle:nil];
    MNDRechargeViewController *reshargeVC = [storyboard instantiateViewControllerWithIdentifier:@"MNDRechargeViewController"];
    [self.navigationController pushViewController:reshargeVC animated:YES];
}

#pragma mark -没有登录，跳转到登录页面

- (void)goToLoginVC {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    UINavigationController *naviVC = [storyboard instantiateInitialViewController];
    
    [self presentViewController:naviVC animated:YES completion:^{
        
    }];
}

- (JhtVerticalMarquee *)verticalMarquee {
    if (!_verticalMarquee) {
       JhtVerticalMarquee *verticalMarquee = [[JhtVerticalMarquee alloc] init];
        _verticalMarquee = verticalMarquee;
    }
    return _verticalMarquee;
}

//点击总资产,跳转到资产统计
- (void)clickBackImgV:(UITapGestureRecognizer *) tapGesture{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    MNDUserAssetViewController *userAssetVC = [storyBoard instantiateViewControllerWithIdentifier:@"MNDUserAssetViewController"];
    [self.navigationController pushViewController:userAssetVC animated:YES];
}

- (void)clickLatest30DaysIncomeAction:(id)sender {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    MNDUserAssetViewController *userAssetVC = [storyBoard instantiateViewControllerWithIdentifier:@"MNDUserAssetViewController"];
    [self.navigationController pushViewController:userAssetVC animated:YES];
}

- (void)loadEncodeUserCodeWithPage:(NSString *)page {
    WeakSelf(self);
    [NetworkProgressHUD showHud];
    
    MNDLoginUserModel *user = [MNDLoginUserModel currentLoginUser];
    
    [APIManager sendRequestWithPath:API_ENCODE_USERCODE parameters:[MNDSign signedParamWithDict:@{@"user_code": user.user_code}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            
            if ([page isEqualToString:@"latest_income"]) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[MNDLatestReturnWebViewController description] bundle:nil];
                    MNDLatestReturnWebViewController *returnVC = [storyboard instantiateViewControllerWithIdentifier:[MNDLatestReturnWebViewController description]];
                    returnVC.url = self.myIncomeModel.return_income_30day_url;
                    returnVC.encodeUserCode = responseModel.data[@"user_code"];
                    [weakself.navigationController pushViewController:returnVC animated:YES];
                });
            } else {
                MNDShareWebViewController *shareWebVC = [[MNDShareWebViewController alloc]init];
                shareWebVC.hidesBottomBarWhenPushed = YES;
                shareWebVC.webViewUrl = [NSString stringWithFormat:@"%@%@", WebBaseURL, kShareFriend];
                shareWebVC.encodeUserCode = responseModel.data[@"user_code"];
                shareWebVC.title = @"邀请有奖";
                [self.navigationController pushViewController:shareWebVC animated:YES];
            }
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

 //隐藏显示总资产点击事件
- (void)clickShowOrHidenMoneyBtn:(UIButton *)btn {
    
    if (btn.selected == NO) {
        self.logInfoView.allMoneyLabel.text = @"****";
        self.logInfoView.currentYieldLabel.text = @"****";
        self.logInfoView.todayMoneyLabel.text = @"****";
        self.logInfoView.accountMoneyLabel.text = @"****";
        self.logInfoView.currentInvestLabel.text = @"****";
        
        btn.selected = YES;
    }else {
        //总资产
        NSString * userAmountAssets = self.userAssetInfo.userAmountAssets;
        if (userAmountAssets.doubleValue <= 0) {
            userAmountAssets = @"0.00";
        }
        self.logInfoView.allMoneyLabel.text = userAmountAssets;
        //已赚收益
        NSString *returnProfit = self.userAssetInfo.userInterestAmount;
        if (returnProfit.doubleValue <= 0) {
            returnProfit = @"0.00";
        }
        self.logInfoView.currentYieldLabel.text = returnProfit;
        //待收益
        NSString *returnAmount = self.userAssetInfo.userWaitInterestAmount;
        if (returnAmount.doubleValue <= 0) {
            returnAmount = @"0.00";
        }
        self.logInfoView.todayMoneyLabel.text = returnAmount;
        NSString *userAmount = self.userAssetInfo.userAmountAvailable;
        if (userAmount.doubleValue <= 0) {
            userAmount = @"0.00";
        }
        self.logInfoView.accountMoneyLabel.text = userAmount;
        NSString *tenderAmount = self.userAssetInfo.userCurrentTenderAmount;
        if (tenderAmount.doubleValue <= 0) {
            tenderAmount = @"0.00";
        }
        self.logInfoView.currentInvestLabel.text = tenderAmount;
        btn.selected = NO;
    }
}

//点击账户余额
- (void)clickAccountRemainView:(UITapGestureRecognizer *)tapGesture {
    self.tabBarController.selectedIndex = 3;
}

//点击当前投资
- (void)clickCurrentInvestView:(UITapGestureRecognizer *)tapGesture {
    MNDRecordGroupViewController *recordVC = [UIStoryboard viewControllerWithIdentifier:@"MNDRecordGroupViewController"];
    [self.navigationController pushViewController:recordVC animated:YES];
//    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"InvestmentRecord" bundle:nil];
//    MNDInvestmentRecordViewController *investmentRecordVC = [storyBoard instantiateViewControllerWithIdentifier:@"MNDInvestmentRecordViewController"];
//    [self.navigationController pushViewController:investmentRecordVC animated:YES];
}

//点击今日回款
- (void)clickBackMoneyView:(UITapGestureRecognizer *)tapGesture {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[MNDBackMoneyRootViewController description] bundle:nil];
    
    MNDBackMoneyRootViewController *returnVC = [storyboard instantiateViewControllerWithIdentifier:[MNDBackMoneyRootViewController description]];
    returnVC.isUnReturn = YES;
    
    [returnVC setHidesBottomBarWhenPushed:YES];

    [self.navigationController pushViewController:returnVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
